package com.examination.config;

/**
 * @author He
 * @version 1.0
 * @Date 2023/2/6 10:38
 * @Desc
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.system.ApplicationHome;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.io.File;

/**
 * 自定义资源映射
 * 设置虚拟路径，访问绝对路径下资源
 */
@Configuration
public class WebConfigurer extends WebMvcConfigurerAdapter {

    @Autowired
    PreReadUploadConfig uploadConfig;
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        //获取jar包所在目录
        ApplicationHome home = new ApplicationHome(getClass());
        File jarF = home.getSource();
        //在jar包所在目录下生成一个upload文件夹用来存储上传的图片
        String dirPath = jarF.getParentFile().toString()+"/files/images/";
        System.out.println("dirpath"+dirPath);
        String jarPath = System.getProperty("user.dir");
        registry.addResourceHandler("/files/images/**")//访问这个请求就会映射到设置的静态资源路径
                .addResourceLocations("file:///"+dirPath);//静态资源实际存放的位置
    }
}